//
//  com_tskiViewController.m
//  12Persistence
//
//  Created by Rocky Xu on 12-8-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "com_tskiViewController.h"

@implementation com_tskiViewController
@synthesize field1;
@synthesize field2;
@synthesize field3;
@synthesize field4;

-(NSString *) dataFilePath{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    for (NSString *string in paths) {
        NSLog(@"strings: %@",string);
    }
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSLog(@"documentsDirectory: %@",documentsDirectory);
    
    NSString *rtString1 = [documentsDirectory stringByAppendingPathComponent:kFilename];
    NSString *rtString2 = [documentsDirectory stringByAppendingPathExtension:kFilename];
    NSLog(@"rtString1: %@",rtString1);
    NSLog(@"rtString2: %@",rtString2);
    
    return rtString1;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    NSString *filePath = [self dataFilePath];
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        field1.text = [array objectAtIndex:0];
        field2.text = [array objectAtIndex:1];
        field3.text = [array objectAtIndex:2];
        field4.text = [array objectAtIndex:3];
        //	field4.text = [[NSString alloc]initWithFormat:@"field4: %@",[array objectAtIndex:3]];
//        field4.text = [[array objectAtIndex:3] stringby
        [array release];
    }
    
    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}


-(void)applicationWillResignActive:(NSNotification *)notification{
    NSMutableArray *array = [[NSMutableArray alloc]init];
    [array addObject:field1.text];
        [array addObject:field2.text];
        [array addObject:field3.text];
        [array addObject:field4.text];
    [array writeToFile:[self dataFilePath]  atomically:YES];
    [array release];
}
- (void)viewDidUnload
{
    self.field1 = nil;
        self.field2 = nil;
        self.field3 = nil;
        self.field4 = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)dealloc{
    [field1 release];
    [field2 release];
    [field3 release];
    [field4 release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
